/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package snippet.lab4;

import java.io.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author hoang.pt
 */
public class Student implements Serializable {

    private String name;
    private String clasx;
    private int maths;
    private int physics;
    private int chemistry;

    public Student() {
    }

    /**
     * constructor
     * @param name
     * @param clasx
     * @param maths
     * @param physics
     * @param chemistry
     */
    public Student(String name, String clasx, int maths, int physics, int chemistry) {
        this.name = name;
        this.clasx = clasx;
        this.maths = maths;
        this.physics = physics;
        this.chemistry = chemistry;
    }

    public int getChemistry() {
        return chemistry;
    }

    public void setChemistry(int chemistry) {
        this.chemistry = chemistry;
    }

    public String getClasx() {
        return clasx;
    }

    public void setClasx(String clasx) {
        this.clasx = clasx;
    }

    public int getMaths() {
        return maths;
    }

    public void setMaths(int maths) {
        this.maths = maths;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getPhysics() {
        return physics;
    }

    public void setPhysics(int physics) {
        this.physics = physics;
    }

    public String toString() {
        return name + ", " + clasx + ", " + maths + ", " + physics + ", " + chemistry;
    }

    /**
     * 
     */
    public void saveStudent() throws FileNotFoundException {

        Scanner input = new Scanner(System.in);

        //input
        System.out.println("Hanoi-Amsterdam");
        System.out.println("Name: ");
        String name = input.next();
        System.out.println("Class: ");
        String clasx = input.next();
        System.out.println("Math: ");
        int maths = input.nextInt();
        System.out.println("Physics");
        int physics = input.nextInt();
        System.out.println("Chemistry");
        int chemistry = input.nextInt();

        //save into object
        Student stu = new Student(name, clasx, maths, physics, chemistry);

  
        try {
            //init writer Object
            FileOutputStream fWrite = new FileOutputStream("student.ser", true);
            ObjectOutputStream oWriter = new ObjectOutputStream(fWrite);
            
            //write
            oWriter.writeObject(stu);

            //close
            oWriter.flush();
            oWriter.close();
            fWrite.close();

        } catch (IOException ex) {
        }

    }

    /**
     * 
     */
    public void displayStudent() throws FileNotFoundException, IOException, ClassNotFoundException {

        //init Reader
        FileInputStream fReader = new FileInputStream("student.ser");
        ObjectInputStream oReader = new ObjectInputStream(fReader);

        //read until null
        Student stuObj;

        stuObj = (Student) oReader.readObject();
        System.out.println(stuObj);

        //close
        oReader.close();
        fReader.close();
    }
}
